package com.woniu.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.his.dao.*;
import com.woniu.his.entity.*;
import com.woniu.his.service.TaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TaoServiceImpl implements TaoService {
    @Autowired
    private HospitalizationInfoMapper hospitalizationInfoMapper;
    @Autowired
    private WardInfoMapper wardInfoMapper;
    @Autowired
    private SickbedInfoMapper sickbedInfoMapper;
    @Autowired
    private PayInfoMapper payInfoMapper;
    @Autowired
    private DrugApplyMapper drugApplyMapper;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<Map<String, Object>> taoFindAllHospitalApplyStatusFalseInfo() {
        return hospitalizationInfoMapper.taoFindAllHospitalApplyStatusFalseInfo();
    }

    @Override
    public List<Map<String, Object>> findWardInfoBySectionId(int sectionId) {
        QueryWrapper<WardInfo> wardInfoQueryWrapper = new QueryWrapper<>();
        wardInfoQueryWrapper.eq("section_id", sectionId);
        List<Map<String, Object>> list = wardInfoMapper.selectMaps(wardInfoQueryWrapper);
        return list;
    }

    @Override
    public List<Map<String, Object>> findEmptySickbedInfoByWardId(int wardId) {
        QueryWrapper<SickbedInfo> sickbedInfoQueryWrapper = new QueryWrapper<>();
        sickbedInfoQueryWrapper.eq("is_null", false);
        sickbedInfoQueryWrapper.eq("ward_id", wardId);
        List<Map<String, Object>> list = sickbedInfoMapper.selectMaps(sickbedInfoQueryWrapper);
        return list;
    }

    @Override
    @Transactional
    public String taoUpdateHospitalInfo(Map<String, Object> map) {
        //这是修改住院信息表的内容
        Integer hospitalApplyId = Integer.valueOf(map.get("hospitalApplyId").toString());
        Integer medicalId = Integer.valueOf(map.get("medicalId").toString());
        String hospitalizationType = map.get("hospitalizationType").toString();
        Integer sectionId = Integer.valueOf(map.get("sectionId").toString());
        Integer wardId = Integer.valueOf(map.get("wardId").toString());
        Integer bedId = Integer.valueOf(map.get("bedId").toString());
        Date inHospitalStartTime = null;
        try {
            inHospitalStartTime = simpleDateFormat.parse(map.get("inHospitalStartTime").toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer hospitalizationDate = Integer.valueOf(map.get("hospitalizationDate").toString());
        HospitalizationInfo hospitalizationInfo = new HospitalizationInfo();
        hospitalizationInfo.setHospitalizationId(hospitalApplyId);
        hospitalizationInfo.setMedicalId(medicalId);
        hospitalizationInfo.setHospitalizationType(hospitalizationType);
        hospitalizationInfo.setSectionId(sectionId);
        hospitalizationInfo.setWardId(wardId);
        hospitalizationInfo.setSickbedId(sectionId);
        hospitalizationInfo.setHospitalizationTime(inHospitalStartTime);
        hospitalizationInfo.setHospitalizationDate(hospitalizationDate);
        hospitalizationInfo.setStatus(1);
        //将修改后的住院信息更新到数据库中
        hospitalizationInfoMapper.updateById(hospitalizationInfo);
        //将病人的床位更换为使用状态
        SickbedInfo sickbedInfo = new SickbedInfo();
        sickbedInfo.setSickbedId(bedId);
        sickbedInfo.setIsNull(1);
        sickbedInfoMapper.updateById(sickbedInfo);
        //生成对应的收费表数据库
        PayInfo payInfo = new PayInfo();
        payInfo.setMedicalId(medicalId);
        payInfo.setPayProject(hospitalizationType);
        payInfo.setCount(hospitalizationDate);
        BigDecimal normalPrice = new BigDecimal(200);
        BigDecimal hardPrice = new BigDecimal(500);
        BigDecimal price = hospitalizationType.equals("普通住院") ? normalPrice : hardPrice;
        payInfo.setPrice(price);
        BigDecimal allDate = new BigDecimal(hospitalizationDate);
        payInfo.setTotalPrice(price.multiply(allDate));
        payInfoMapper.insert(payInfo);
        return "操作成功";
    }

    //这是查询出所有可以出院的住院表信息,判断条件为已处理并且出院状态为否的信息
    @Override
    public List<Map<String, Object>> taoFindAllOutHospitalApplyInfo() {
        List<Map<String, Object>> list = hospitalizationInfoMapper.taoFindAllOutHospitalApplyInfo();
        return list;
    }

    @Override
    @Transactional
    public String updateHospitalInfoAddOutHospitalStatus(int hosipitalizationInfoId) {
        HospitalizationInfo hospitalizationInfo = hospitalizationInfoMapper.selectById(hosipitalizationInfoId);
        hospitalizationInfo.setIsOut(1);
        hospitalizationInfo.setOutTime(new Date());
        //获取病床Id清空病床
        Integer sickbedId = hospitalizationInfo.getSickbedId();
        SickbedInfo sickbedInfo = new SickbedInfo();
        sickbedInfo.setIsNull(0);
        sickbedInfo.setSickbedId(sickbedId);
        sickbedInfoMapper.updateById(sickbedInfo);
        hospitalizationInfoMapper.updateById(hospitalizationInfo);
        return "办理成功";
    }

    //这是查询出药品申请单只属于住院,手术,以及手术及住院的申请
    @Override
    public List<Map<String, Object>> taoFindAllDrugApplyByMedicalIsHospitalization() {
        List<Map<String, Object>> list = hospitalizationInfoMapper.taoFindAllDrugApplyByMedicalIsHospitalization();
        return list;
    }

    @Override
    @Transactional
    public String updateDrugApplyInfoById(int drugApplyId) {
        DrugApply drugApply = new DrugApply();
        drugApply.setDrugApplyId(drugApplyId);
        drugApply.setIsTake(1);
        drugApply.setTakeTime(new Date());
        drugApplyMapper.updateById(drugApply);
        return "操作成功";
    }
}
